import Common from '../../../common/common.js';
let common = new Common;
const app = getApp()
Page({
  data: {
    goodsListPT: [],//普通会员价
    goodsListJT: [],//阶梯会员价
    activeTime: '', //活动时间
    activeExplain: '参加此活动后，部分商品将支持会员价售卖。',
    uuid: '',
    status: '',
    activityDesc: '', //活动描述
    beginTime: '',
    endTime: '',
    storeStatu: '', //参加或退出
    storeActivityId: "",
    type: '',
    activityGoodsIds: [],
    signOut: [],
    activityGoodsIdPT: [], //普通会员参加id
    storeActivityIdPT: [],//普通会员退出id
    arr: [],
  },
  // 选中的阶梯会员价
  radioChange(e) {
    let index = e.currentTarget.dataset.index;
    let activitygoodsid = e.currentTarget.dataset.activitygoodsid;
    let arr = this.data.arr || [];
    console.log(index)
    arr[index] = arr[index] == activitygoodsid ? '' : activitygoodsid;
    this.setData({
      arr,
      activityGoodsIds: arr
    })
    console.log(this.data.activityGoodsIds)
  },
  onLoad: function (options) {
    let that = this
    that.setData({
      uuid: options.uuid,
      type: options.type
    })
    that.getActiveDetails() //活动详情接口
  },
  // 活动详情接口
  getActiveDetails: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    let data = {
      uuid: that.data.uuid,
      orgSeq: app.globalData.orgSeq, //orgSeq 门店id
    };
    common.requestAjax('/api/goods-service/v1/goodsShopActivity/getActivityInfo', 'GET', data).then(res => {
      if (res.data.code == 1) {
        wx.hideLoading();
        // 阶梯会员的id
        let arrayJTSigOut = []
        res.data.result.goodsListJT.forEach((item, id) => {
          item.goodsActivityGoodsPTAndJTS.forEach((items, index) => {
            arrayJTSigOut.push(items.storeActivityId)
          })
        })
        // 注：去除null字符，IE9(不包含IE9)以下的版本没有trim()方法
        var status = arrayJTSigOut.filter(function (s) {
          return s && s.trim();
        });
        // 普通会员的id
        let arrayPTJion = []
        let arrayPTSigOut = []
        res.data.result.goodsListPT.forEach((item, index) => {
          item.goodsActivityGoodsPTAndJTS.forEach((items, index) => {
            arrayPTJion.push(items.activityGoodsId)
            arrayPTSigOut.push(items.storeActivityId)
          })
        })
        var statusPT = arrayPTSigOut.filter(function (s) {
          return s && s.trim();
        });
        console.log(arrayPTJion)
        console.log(status)
        this.setData({
          goodsListPT: res.data.result.goodsListPT, //普通商品数组 
          goodsListJT: res.data.result.goodsListJT, //阶梯商品数组 
          activityDesc: res.data.result.activityDesc, //活动描述
          beginTime: res.data.result.beginTime.substr(0, 10), //开始时间
          endTime: res.data.result.endTime.substr(0, 10), //结束时间
          // storeStatus: res.data.result.storeStatus, //1我要退出   0或null我要参加
          storeStatus: status.length || statusPT.length >= 1 ? 1 : '0', //1我要退出0或null我要参加
          storeActivityId: res.data.result.storeActivityId,
          activityGoodsId: res.data.result.activityGoodsId,//活动id
          signOut: status,//我要退出id
          activityGoodsIds: [],// 参加活动的id
          activityGoodsIdPT: arrayPTJion, //普通会员参加id
          storeActivityIdPT: arrayPTSigOut//普通会员退出id
        })
      } else {
        common.showToast(res.data.message);
      }
    }).catch(err => {
      common.showToast(err);
    })
  },
  onShow: function () {

  },
  // 参加或退出活动
  join: function () {
    // id==1 我要退出  0或null 我要参加  
    let that = this
    if (app.globalData.openStatus == 0) {
      wx.showToast({
        title: '该店铺已歇业，无法参加活动',
        icon: 'none'
      })
      return;
    }
    if (that.data.storeStatus == 1) {
      this.setData({
        contentTip: '请确认退出此活动',
        status: 0, //0表示退出
      })
    } else if (that.data.storeStatus == 0 || that.data.storeStatus == null) {
      this.setData({
        contentTip: '请确认参加此活动',
        status: 1, //1表示加入
      })
    }
    wx.showModal({
      title: '提示',
      content: that.data.contentTip,
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          let data = {
            "uuid": that.data.storeActivityId, //店铺参加活动得uuid，会在详情接口返回字段名称storeActivityId，如果为空则表示为店铺参加活动
            "status": that.data.status, //1-表示加入  0表示退出 
            "orgSeq": app.globalData.orgSeq, //orgSeq 门店id
            "activityId": that.data.uuid,//活动id 
          };
          if (that.data.status == 0) {   //我要退出
            data.storeActivityId = that.data.storeActivityIdPT.concat(that.data.signOut)//退出id
          } else if (that.data.status == 1) { //我要参加 
            var parkingList = that.data.activityGoodsIds; //去除阶梯数组空字符串
            for(var i = 0;i<parkingList.length;i++){
              if(parkingList[i]==''||parkingList[i]==null||typeof(parkingList[i])==undefined){
                  parkingList.splice(i,1);
                  i=i-1;
              }
            }
            console.log(that.data.activityGoodsIdPT)
            console.log(parkingList)
            if (that.data.activityGoodsIdPT.length == 0 && parkingList.length == 0) {
              common.showToast('请选择要参加的会员价商品')
              return
            }
            data.activityGoodsId = that.data.activityGoodsIdPT.concat(that.data.activityGoodsIds)//参加id
          }
          console.log(data)
          common.requestAjax('/api/goods-service/v1/goodsShopActivity/editStoreParticipation', 'POST', data).then(res => {
            if (res.data.code == 1) {
              if (that.data.status == 1) {
                setTimeout(() => {
                  common.showToast('已参加该活动')
                }, 500)
                that.setData({
                  storeStatus: 1
                })
              } else {
                setTimeout(() => {
                  common.showToast('已退出该活动')
                }, 500)
                that.setData({
                  storeStatus: 0,
                  arr:[], //置空选中
                })
              }
              that.getActiveDetails() //活动详情接口
            } else {
              common.showToast(res.data.message);
            }
          }).catch(err => {
            console.log(err)
            //common.showToast(err);
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
})